package yl.hs.bmipfull.service.material.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.material.IPurchaseContractPriceAdjustDocDao;
import yl.hs.bmipfull.daos.material.IPurchaseContractPriceAdjustmentDao;
import yl.hs.bmipfull.daos.material.IPurchaseContractProductDao;
import yl.hs.bmipfull.pojo.material.PurchaseContractPriceAdjustDocEntity;
import yl.hs.bmipfull.pojo.material.PurchaseContractPriceAdjustmentEntity;
import yl.hs.bmipfull.pojo.material.PurchaseContractProductEntity;
import yl.hs.bmipfull.pojo.salemanage.ContractPriceAdjustDocEntity;
import yl.hs.bmipfull.pojo.salemanage.ContractPriceAdjustmentEntity;
import yl.hs.bmipfull.service.material.IPurchaseContractPriceAdjustDocService;
import yl.hs.bmipfull.service.material.IPurchaseContractPriceAdjustmentService;
import yl.hs.bmipfull.service.salemanage.impl.SaleBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.material.PurchaseContractPriceAdjustVO;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作用:  销售合同调价单服务层接口实现<br/>
 * 创建时间: 2021年06月03日 09时12分21秒 <br/>
 *
 * @author 张凯
 */
@Service
public class PurchaseContractPriceAdjustDocServiceImpl extends SaleBaseService implements IPurchaseContractPriceAdjustDocService {

    @Autowired
    IPurchaseContractPriceAdjustDocDao dao;

    @Autowired
    IPurchaseContractPriceAdjustmentService adjustmentService;
    @Autowired
    IPurchaseContractPriceAdjustmentDao adjustmentdao;
    @Override
    public List<PurchaseContractPriceAdjustDocEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(PurchaseContractPriceAdjustDocEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public PageResult<PurchaseContractPriceAdjustDocEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(PurchaseContractPriceAdjustDocEntity.class);

        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<PurchaseContractPriceAdjustDocEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),PurchaseContractPriceAdjustDocEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }

    @Override
    public PageResult<PurchaseContractPriceAdjustVO> queryPageView(PageQuery pageQuery, int rowNum, int page) {
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<PurchaseContractPriceAdjustVO> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),PurchaseContractPriceAdjustDocEntity.class);
        result.setRecords(dao.queryCountByConditionView(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPageView(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public PurchaseContractPriceAdjustDocEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveContractPriceAdjustDoc(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        PurchaseContractPriceAdjustDocEntity entity = new PurchaseContractPriceAdjustDocEntity();
        if (Tl.isEmpty(sn)) {
            //执行保存
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            sn = queryNewSN(PurchaseContractPriceAdjustDocEntity.class);
            entity.setSn(sn);
            entity.setHandlerTime(new Date());
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入采购合同调价单信息", Tl.toJson(entity), PurchaseContractPriceAdjustDocEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            var changlog= Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setHandlerTime(new Date());
            result = dao.update(entity);

            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changlog, Tl.toJson(entity), PurchaseContractPriceAdjustDocEntity.class);
            }
        }
        // 调价单明细信息
        if (result > 0) {
            List<Map<String, Object>> adjustment = (List<Map<String, Object>>) model.get("adjustment");
            if (adjustment != null) {
                adjustmentService.saveContractPriceAdjustment(adjustment, sn);
            }

        }
        return result;
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, PurchaseContractPriceAdjustDocEntity.class);
        }
        return result;
    }

    @Override
    public Integer PriceAdjustByProduction(Map<String, Object> model, String sn)throws Exception {
        var user= HsApp.getCurrent();
        Integer result = 0;
        var contractSNList=PurchaseproductsDao.queryListByRmSN(sn);
        if (contractSNList !=null && contractSNList.size()>0){
            for (var contractSN:contractSNList) {
                PurchaseContractPriceAdjustDocEntity doc = new PurchaseContractPriceAdjustDocEntity();
                doc.setHandlerSN(user.getUserSN());
                doc.setHandlerName(user.getUserName());
                doc.setHandlerTime(new Date());
                doc.setContractSN(contractSN.getContractSn());
                doc.setSn(queryNewSN(PurchaseContractPriceAdjustDocEntity.class));
                doc.setState((short) 2);
                doc.setRemark("根据产品统一调价");
                dao.insertNew(doc);
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                var startTimeStr=(String) model.get("startTime");
                var startTime=sdf.parse(startTimeStr);
                //子表
                PurchaseContractPriceAdjustmentEntity entity = new PurchaseContractPriceAdjustmentEntity();
                entity.setAdjustDocSN(doc.getSn());
                entity.setSn(queryNewSN(PurchaseContractPriceAdjustmentEntity.class));
                entity.setContractProductSN(contractSN.getRmSN());
                entity.setCause((String) model.get("cause"));
                entity.setOriginalPrice(adjustmentService.getProductPrice(contractSN.getContractSn(),contractSN.getRmSN(),new Date()));
                if(model.get("modifyPrice") != null){
                    BigDecimal modifyPrice= BigDecimal.valueOf(Double.parseDouble(model.get("modifyPrice").toString()));
                    entity.setModifyPrice(modifyPrice.floatValue());
                    entity.setCurrentPrice(entity.getOriginalPrice()+modifyPrice.floatValue());
                }else{
                    return -1;
                }
                entity.setStartTime(startTime);
                result=result+adjustmentdao.insertNew(entity);
            }
        }
        return result;
    }
    @Autowired
    IPurchaseContractProductDao PurchaseproductsDao;
}